//
//  UILabel+NameRole.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/20.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "UILabel+NameRole.h"

@implementation UILabel (NameRole)

- (void)setName:(NSString *)name withNameColor:(UIColor *)nameColor withNameFont:(UIFont *)nameFont andRole:(NSString *)role withRoleColor:(UIColor *)roleColor withRoleFont:(UIFont *)roleFont
{
    if (nil == name || 0 == name.length) return;
    
    NSString *strSpace = @" ";
    if (nil == role || 0 == role.length) {
        strSpace = @"";
        role = @"";
    }
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@", name, strSpace, role]];
    NSRange rangeName = NSMakeRange(0, name.length);
    [attrStr addAttribute:NSFontAttributeName value:nameFont range:rangeName];
    [attrStr addAttribute:NSForegroundColorAttributeName value:nameColor range:rangeName];
    
    if (nil != role && 0 != role.length) {
        NSRange rangeRole = NSMakeRange(name.length+1, role.length);
        [attrStr addAttribute:NSFontAttributeName value:roleFont range:rangeRole];
        [attrStr addAttribute:NSForegroundColorAttributeName value:roleColor range:rangeRole];
    }
    self.attributedText = [attrStr copy];
}

@end
